package org.myOrg.spetrum.GUI;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.myOrg.spetrum.Listener.AddBugModeListener;
import org.myOrg.spetrum.util.Message;
import org.myOrg.spetrum.util.MessageImplement;
import org.myOrg.spetrum.util.MessageMangement;
import org.myOrg.spetrum.util.MessageMangementImplement;
import org.myOrg.spetrum.util.OverallFactory;
import org.myOrg.spetrum.util.Sender;

public class BugInsertControlPanel extends JPanel implements ActionListener , Sender{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JButton done;
	private JButton cancel;
	private JButton next;
	private JTextField textParam;
	private JTextField textValue;
	private JLabel tip;
	
	private MessageMangement mangerment;

	public BugInsertControlPanel() {
		this.setMessageMangement(OverallFactory.getMessageManger());
		build();
		setListener();
	}

	private void build() {
		this.setBorder(BorderFactory.createTitledBorder("add the bug mode"));
		JPanel configurePanel = buildConfigurePanel();
		tip = new JLabel("state:");
		this.setLayout(new BorderLayout());
		this.add(tip, BorderLayout.NORTH);
		this.add(configurePanel, BorderLayout.SOUTH);
		this.setVisible(false);
	}

	private JPanel buildConfigurePanel() {
		JPanel configurePanel = new JPanel();
		configurePanel.setLayout(new FlowLayout(FlowLayout.LEADING));
		JLabel param = new JLabel("param id:");
		textParam = new JTextField();
		textParam.setColumns(2);
		JLabel value = new JLabel("value:");
		textValue = new JTextField();
		textValue.setColumns(2);
		next = new JButton("next");
		done = new JButton("done");
		cancel = new JButton("cancel");
		configurePanel.add(param);
		configurePanel.add(textParam);
		configurePanel.add(value);
		configurePanel.add(textValue);
		configurePanel.add(next);
		configurePanel.add(cancel);
		configurePanel.add(done);
		return configurePanel;
	}

	public void setListener() {
		this.cancel.addActionListener(this);
		this.next.addActionListener(this);
		this.done.addActionListener(this);
		this.done.addActionListener(new AddBugModeListener());
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JFrame frame = new JFrame();
		frame.setSize(500, 150);
		frame.getContentPane().setLayout(new BorderLayout());
		frame.getContentPane().add(new BugInsertControlPanel(),
				BorderLayout.NORTH);
		frame.setVisible(true);
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getActionCommand().equals("next"))
			performNext();
		else if (e.getActionCommand().equals("cancel")||e.getActionCommand().equals("done"))
			performCancel();
	}

	private void performNext() {
		int param = Integer.parseInt(this.textParam.getText());
		int value = Integer.parseInt(this.textValue.getText());
		String tip = this.tip.getText();
		if (tip.length() <= 7)
			this.tip.setText(tip + " " + param + ": " + value);
		else
			this.tip.setText(tip + "--> " + param + ": " + value);
		
   		Message msg = new MessageImplement(this,
				MessageMangementImplement.ADDTEMPVALUE, new int[]{param , value});
		this.sendMessage(msg);

		this.textParam.setText("");
		this.textValue.setText("");
	}

	private void performCancel() {
		this.tip.setText("State: ");
		this.textParam.setText("");
		this.textValue.setText("");
		this.setVisible(false);
	}

	@Override
	public void sendMessage(Message msg) {
		// TODO Auto-generated method stub
		this.mangerment.dispatchMeesage(msg);
	}

	@Override
	public void setMessageMangement(MessageMangement mangerment) {
		// TODO Auto-generated method stub
		this.mangerment = mangerment;
	}
}
